package com.jetpackframework.retrofit;

import com.jetpackframework.ftp.FTPTask;
import com.jetpackframework.ftp.FtpUtil;
import com.jetpackframework.rxjetpack.Dispose;
import com.jetpackframework.rxjetpack.DisposeImpl;
import com.jetpackframework.rxjetpack.Observable;
import com.jetpackframework.rxjetpack.Observer;
import com.jetpackframework.rxjetpack.RxJetpack;

import java.util.Objects;

public class ObservableFTP extends Observable<FTPTask> {
    private FTPTask task;

    public ObservableFTP(FTPTask task) {
        this.task = task;
    }

    public static Observable<FTPTask> ftp(FTPTask task) {
        Objects.requireNonNull(task);
        return RxJetpack.onAssembly(new ObservableFTP(task));
    }

    @Override
    public void subscribeActual(Observer<? super FTPTask> observer) {
        FTPObserver ftpObserver = new FTPObserver(observer,task);
        ftpObserver.onSubscribe(new DisposeImpl());
        ftpObserver.run();
    }

    private static class FTPObserver implements Observer<FTPTask>,Runnable{

        private Observer<? super FTPTask> observer;
        private FTPTask task;

        public FTPObserver(Observer<? super FTPTask> observer, FTPTask task) {
            this.observer = observer;
            this.task = task;
        }

        @Override
        public void onSubscribe(Dispose dispose) {
            observer.onSubscribe(dispose);
        }

        @Override
        public void onNext(FTPTask task) {

            FtpUtil.getInstance().send(task);
        }

        @Override
        public void onError(Throwable t) {

        }

        @Override
        public void onComplete() {

        }

        @Override
        public void run() {
            try {

            }catch (Exception e){

            }
        }
    }
}
